/*请求协议设计列表*/

var num = 1;

function getProtocolDesignList(args) {
	$.ajax({
		url: HOST.concat("/protocolDesigns/username?token=") + config.token,
		type: 'GET',
		data: {
			"username": config.username,
			"pageNum": args,
			"pageSize": "10"
		},
		dataType: "json",
		success: function(datas) {
			checkLogin(datas);
			console.log(datas);
			//获取查询状态
			var flag = datas.meta.state;
			if(flag == "success") {
				//查询成功
				//获取返回的数据
				var objs = datas.data;

				if(objs.length != 0) {
					var content = $("#protocolDesignList");
					//清空id为protocolDesignList的div
					content.empty();

					for(var i = 0; i < objs.length; i++) {

						//行div
						var div1 = $("<div id='" + objs[i].id + "'></div>");
						div1.addClass('tr clearfix border-bottom-none');

						//多选框
						var div2 = $("<div></div>");
						div2.addClass("td w5");

						var myInput = $("<input type='checkbox' name='check' />");
						myInput.val(objs[i].id);
						div2.append(myInput);
						div1.append(div2);

						//协议编号列  协议的id
						var id = objs[i].id;
						var protocol_id = "D-" + (id.substr(6,2)+id.substr(-6)).toUpperCase();
						var div3 = $("<div class='td w10'></div>");
						div3.text(protocol_id);
						div1.append(div3);

						//通用电力协议名
						var protocol_name = objs[i].protocolName;
						var div4 = $("<div class='td w20'></div>");
						div4.text(protocol_name);
						div1.append(div4);

						//安全电力协议名
						var protocol_designName = objs[i].designName;
						var div5 = $("<div class='td w15'></div>");
						div5.text(protocol_designName);
						div1.append(div5);

						//创建时间
						var protocol_createTime = objs[i].createTime;
						var div6 = $("<div class='td w20'></div>");
						div6.text(protocol_createTime);
						div1.append(div6);

						//备注
						var info = objs[i].other;
						var div7 = $("<div class='td w20'></div>");
						if(info == "") {
							info = "无";
						}
						div7.text(info);
						div1.append(div7);

						//操作按钮
						var div8 = $("<div class='td w10'></div>");

						var div9 = $("<div style='display: inline-block;' class='myIcon'></div>");
						var a1 = $("<a href='#' onclick='findProDesgin(\""+ objs[i].id+"\",\""+objs[i].protocolName+"\")' ><i class='fa fa-search fa-lg' title='查看/修改协议设计'></i></a>");
						div9.append(a1);

						var div10 = $("<div style='display: inline-block;' class='myIcon'></div>");
						var a2 = $("<a href='javascript:void(0)' onclick='delProtocolDesignById(\"" + objs[i].id + "\")'><i class='fa fa-trash fa-lg' title='删除这项设计'></i></a>");
						div10.append(a2);

						/*协议配置*/
						//id为designId
						var div100 = $("<div style='display: inline-block;' class='myIcon'></div>");
						var a4 = $("<a href='configurationDetail.html?id="+objs[i].id+"'><i class='fa fa-reorder fa-lg' title='协议配置'></i></a>");
						div100.append(a4);

						/*编码设计*/
						var div99 = $("<div style='display: inline-block;' class='myIcon myEncodeDesign'></div>");
						var a3 = $("<a href='javascript:void(0)' id='"+objs[i].id+"' onclick='encodeProtocol(\""+objs[i].id+"\")'><i class='fa fa-gear fa-lg' title='编码设计'></i></a>");
						div99.append(a3);

						div8.append(div9);
						div8.append(div100);
						//div8.append(div99);
						div8.append(div10);

						div1.append(div8);

						//向div里面添加一行记录
						$("#protocolDesignList").append(div1);

						//设置页码
						$("#pageNum").text(num);

					}

				}else{
					if(num>1){
						num--;
						//设置页码
						$("#pageNum").text(num);
					}
					alert("已经是最后一页！");
				}

			} else {
				console.log("查询失败");

			}
		}

	});
}

getProtocolDesignList(num);

/*上一页*/
$("#dLastPage").click(function() {
	if(num == 1) {
		alert("已经是第一页!");
	} else {
		num--;
		getProtocolDesignList(num);
	}
});

/*下一页*/
$("#dNextPage").click(function() {
	num++;
	getProtocolDesignList(num);
});

//首页
$("#theFirstPage").click(function(){
	num=1;
	getProtocolDesignList(num);
});
